#include "dmr_gpio.h"



void dmr_gpioWritePinLevel(GPIO_Module* gpiox,uint16_t pin,bool status)
{
	if(status)
	{
		gpiox->PBSC = pin;
	}
	else
	{
		gpiox->PBC = pin;
	}
}

bool dmr_gpioReadPinLevel(GPIO_Module* gpiox,uint16_t pin)
{
	if ((gpiox->PID & pin) != (uint32_t)PIN_RESET)
	{
		return DMR_PIN_SET;
	}
		
	return DMR_PIN_RESET;
}


void dmr_gpioTogger(dmr_gpioHandle *handle)
{
	handle->gpiox->POD ^= handle->pin;
}



void dmr_gpioInit(dmr_gpioHandle *handle)
{
	uint32_t position = 0x00U;
    uint32_t current_pin = 0x00U;
	
	if(handle->gpiox == GPIOA)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
	}
	else if(handle->gpiox == GPIOB)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOB);
	}
	else if(handle->gpiox == GPIOC)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOC);
	}
	else if(handle->gpiox == GPIOD)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOD);
	}

    while(((handle->pin) >> position) != 0)
    {

        current_pin = (handle->pin) & (IO_POSITION_MASK << position);
        if(current_pin)
        {
            GPIO_Alternate_Set(handle->gpiox, handle->alternate, position);

            GPIO_Mode_Set(handle->gpiox, handle->mode, position);

            GPIO_Pull_Set(handle->gpiox, handle->pull, position);

            GPIO_SlewRate_Set(handle->gpiox, handle->speed, position);

            GPIO_Driver_Set(handle->gpiox, handle->current, position);
			
			if(handle->initLevel != DMR_PIN_INIT_LEVEL_DEFAULT)
			{
				dmr_gpioWritePinLevel(handle->gpiox,1 << position,handle->initLevel);
			}
        }
		
		
        position++;
    }
}







